//
//  UIImageView+Ext.swift
//  TTCOM
//
//  Created by Toj on 3/29/23.
//

import UIKit

public extension UIImageView {
    
    /**
     * 生成一个虚线图片
     * size: 上下文图大小/ 图片大小, 一般View多大上下文就是多大
     * color: 虚线颜色
     * lineSize: 虚线宽度, 高度
     * space： 虚线之间间距, 默认与实线一样宽
     */
    func st_dottedLine(size: CGSize = .zero, color: UIColor, lineSize: CGSize, space: CGFloat = 0) {
        let isize = size == .zero ? frame.size : size
        image = UIImage.st_dottedLine(size: isize, color: color, lineSize: lineSize, space: space)
    }
    
    func st_corner(radius: CGFloat, named: String) {
        guard !named.isEmpty else { return }
        let bs = bounds
        DispatchQueue.global(qos: .background).async {
            UIGraphicsBeginImageContextWithOptions(bs.size, false, 0)
            let path = UIBezierPath(roundedRect: bs, cornerRadius: radius)
            path.addClip()
            let img = UIImage(named: named)
            img?.draw(in: bs)
            let cimage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            DispatchQueue.main.async {
                self.image = cimage
            }
        }
    }
    
    var st_corner: CGFloat {
        set {
            UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)
            let path = UIBezierPath(roundedRect: bounds, cornerRadius: newValue)
            path.addClip()
            image?.draw(in: bounds)
            let cimage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            image = cimage
            layer.cornerRadius = newValue
        }
        get {
            return layer.cornerRadius
        }
    }
}
